package com.ruoyi.web.controller.campus;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.campus.domain.MyCollect;
import com.ruoyi.campus.service.IMyCollectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收藏信息Controller
 *
 * @author zht
 * @date 2021-12-07
 */
@RestController
@RequestMapping("/campus/collect")
public class MyCollectController extends BaseController
{
    @Autowired
    private IMyCollectService myCollectService;

    /**
     * 查询收藏信息列表
     */
    @PreAuthorize("@ss.hasPermi('campus:collect:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyCollect myCollect)
    {
        startPage();
        List<MyCollect> list = myCollectService.selectMyCollectList(myCollect);
        return getDataTable(list);
    }

    /**
     * 导出收藏信息列表
     */
    @PreAuthorize("@ss.hasPermi('campus:collect:export')")
    @Log(title = "收藏信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MyCollect myCollect)
    {
        List<MyCollect> list = myCollectService.selectMyCollectList(myCollect);
        ExcelUtil<MyCollect> util = new ExcelUtil<MyCollect>(MyCollect.class);
        return util.exportExcel(list, "收藏信息数据");
    }

    /**
     * 获取收藏信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('campus:collect:query')")
    @GetMapping(value = "/{collId}")
    public AjaxResult getInfo(@PathVariable("collId") String collId)
    {
        return AjaxResult.success(myCollectService.selectMyCollectByCollId(collId));
    }

    /**
     * 新增收藏信息
     */
    @PreAuthorize("@ss.hasPermi('campus:collect:add')")
    @Log(title = "收藏信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyCollect myCollect)
    {
        return toAjax(myCollectService.insertMyCollect(myCollect));
    }

    /**
     * 修改收藏信息
     */
    @PreAuthorize("@ss.hasPermi('campus:collect:edit')")
    @Log(title = "收藏信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyCollect myCollect)
    {
        return toAjax(myCollectService.updateMyCollect(myCollect));
    }

    /**
     * 删除收藏信息
     */
    @PreAuthorize("@ss.hasPermi('campus:collect:remove')")
    @Log(title = "收藏信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{collIds}")
    public AjaxResult remove(@PathVariable String[] collIds)
    {
        return toAjax(myCollectService.deleteMyCollectByCollIds(collIds));
    }

    /**
     * 根据comId和userId查询是否收藏该商品(小程序端接口)
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @GetMapping("/checkIsCollect")
    public AjaxResult checkIsCollect(@RequestParam("comId") String comId,@RequestParam("userId") String userId){
        return toAjax(myCollectService.checkIsCollect(comId,userId));
    }

    /**
     * 收藏该商品(小程序端接口)
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @GetMapping("/toCollect")
    public AjaxResult toCollect(@RequestParam("comId") String comId,@RequestParam("userId") String userId){
        return toAjax(myCollectService.toCollect(comId,userId));
    }

    /**
     * 取消收藏该商品(小程序端接口)
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @GetMapping("/cancelCollect")
    public AjaxResult cancelCollect(@RequestParam("comId") String comId,@RequestParam("userId") String userId){
        return toAjax(myCollectService.cancelCollect(comId,userId));
    }

    /**
     * 查询个人收藏(小程序端接口)
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @GetMapping("/selectMyCollectByUserId")
    public TableDataInfo selectMyCollectByUserId(@RequestParam("userId") String userId){
        startPage();
        List<Map<String,Object>> list = myCollectService.selectMyCollectByUserId(userId);
        return getDataTable(list);
    }
}
